package com.arica.common.utils.file;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;


/**
 * 分布式文件系统工具类
 *
 * @author soo
 */
@Slf4j
@Component
public class FastDFSClientUtil {

    @Value("${fdfs.reqHost}")
    private String reqHost;

    @Value("${fdfs.reqPort}")
    private String reqPort;

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;


    public UploadFileResponse uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = null;
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        // 上传并保存图片，参数： 1-上传的文件流  2-文件的大小  3-文件的后缀 4-可以不管他
        if (Arrays.asList(MimeTypeUtils.IMAGE_EXTENSION).contains(extension)) {
            storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(),file.getSize(), extension,null);

            // 缩略图地址
            String thumbImagePath = thumbImageConfig.getThumbImagePath(storePath.getPath());
            log.info("缩略图地址：【{}】；文件地址：【{}】", thumbImagePath, storePath.getFullPath());

        } else {
            storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        }

        // 带分组的路径 group1/M00/00/00/xxxx.jpg
        log.info(storePath.getFullPath());
        // 不带分组的路径 M00/00/00/xxxx.jpg
        log.info(storePath.getPath());

        //我们在浏览器内访问： 192.168.6.106/group1/M00/00/00/xxxxx.jpg
        //如果配置了域名，则可以通过域名访问，比如：image.baidu.com/group1/M00/00/00/xxxxx.jpg
        return getResAccessUrl(storePath);
    }

    /**
     * 删除文件
     * @param path
     */
    public void delFile(String path) {
        storageClient.deleteFile(path);
    }

    /**
     * 下载文件
     * @param groupName
     * @param path
     * @return
     */
    public InputStream download(String groupName, String path ) {
        InputStream ins =  storageClient.downloadFile(groupName, path, ins1 -> {
            // 将此ins返回给上面的ins
            return ins1;
        }) ;
        return ins ;
    }

    /**
     * 封装文件完整URL地址
     * @param storePath
     * @return
     */
    private UploadFileResponse getResAccessUrl(StorePath storePath) {
        String prefix = "http://" + reqHost + ":" + reqPort + File.separator;
        UploadFileResponse res = new UploadFileResponse();

        String filePath = prefix + storePath.getFullPath();
        String thumbImagePath = prefix + storePath.getGroup() + File.separator + thumbImageConfig.getThumbImagePath(storePath.getPath());

        res.setFilePath(filePath);
        res.setThumbImagePath(thumbImagePath);
        return res;
    }

}